package com.example.constellation.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.constellation.Activity.fg1listActivity;
import com.example.constellation.Adapter.fg1adapter;
import com.example.constellation.Adapter.fg1pageradapter;
import com.example.constellation.R;
import com.example.constellation.bean.StarBean;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment1 extends Fragment {
    private ViewPager fg1vp;
    private GridView fg1gv;
    private LinearLayout fg1ly;
    private List<StarBean.StarinfoBean> mdata;
    private fg1adapter fg1adapter;
    //viewpager图片数组
    private int[] imgs={R.mipmap.pic_guanggao,R.mipmap.pic_star};
    //viewpager数据源
    List<ImageView> imageViews;
    //小圆点集合
    List<ImageView> points;
    private fg1pageradapter fg1pageradapter;


    //完成定时装置，实现自动滑动
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what==1) {
                //获取当前viewpager显示的页面
                int currentItem = fg1vp.getCurrentItem();
                //判断是否为最后一张，是显示第一张，不是显示下一张
                if (currentItem==imageViews.size()-1) {
                    fg1vp.setCurrentItem(0);
                }else {
                    currentItem++;
                    fg1vp.setCurrentItem(currentItem);
                }
                //形成循环发送接收的效果
                handler.sendEmptyMessageDelayed(1,5000);
            }
        }
    };

    public Fragment1() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_1, container, false);
        initview(view);
        Bundle bundle = getArguments();
        StarBean infoBean= (StarBean) bundle.getSerializable("info");
        mdata = infoBean.getStarinfo();//获取关于星座信息的集合数据
        //创建适配器
        fg1adapter = new fg1adapter(getContext(), mdata);
        fg1gv.setAdapter(fg1adapter);
        vgshow();
        vplinstner();
        gvlinstner();
        handler.sendEmptyMessageDelayed(1,5000);
        return view;
    }
//GridView的监听事件
    private void gvlinstner(){
        fg1gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                StarBean.StarinfoBean bean = mdata.get(position);
                Intent intent = new Intent(getContext(), fg1listActivity.class);
                intent.putExtra("star",bean);
                startActivity(intent);
            }
        });
    }


    private void vgshow() {
        imageViews=new ArrayList<>();
        points=new ArrayList<>();
        for (int i=0;i<imgs.length;i++){
            ImageView imageView = new ImageView(getContext());
            imageView.setImageResource(imgs[i]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            //设置图片view的宽高
            LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(lp);
            //将图片view加载到集合中
            imageViews.add(imageView);
            //创建小圆点
            ImageView piv = new ImageView(getContext());
            piv.setImageResource(R.mipmap.point_normal);
            LinearLayout.LayoutParams plp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT
                    , LinearLayout.LayoutParams.WRAP_CONTENT);
            plp.setMargins(20,0,0,0);
            piv.setLayoutParams(plp);
            //将小圆点添加到布局
            fg1ly.addView(piv);
            //便于操作将小圆点添加到统一管理和集合中
            points.add(piv);
        }
        //默认第一个小圆点是获取焦点状态
        points.get(0).setImageResource(R.mipmap.point_focus);
        fg1pageradapter = new fg1pageradapter(getContext(), imageViews);
        fg1vp.setAdapter(fg1pageradapter);
    }

    //viewpager显示页面监听
    private void vplinstner() {
        fg1vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                for (int i=0;i<points.size();i++){
                    points.get(i).setImageResource(R.mipmap.point_normal);
                }
                points.get(position).setImageResource(R.mipmap.point_focus);
            }
        });
    }

    private void initview(View view) {
        fg1vp=view.findViewById(R.id.fg1vp);
        fg1gv=view.findViewById(R.id.fg1gv);
        fg1ly=view.findViewById(R.id.fg1ly);
    }
}
